<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- http://hge.relishgames.com -->

<html>

<head>
<meta name="Keywords" content="game engine, 2d, hardware accelerated, hge, engine, relish games, game development">
<meta name="Description" content="Haaf's Game Engine - Hardware accelerated 2D games engine">
<title>Haaf's Game Engine - Hardware accelerated 2D games engine</title>
<link rel=stylesheet type=text/css href=hge.css>
<script language="JavaScript" src="hge.js"></script>
</head>

<body onload="setContents('cnt_tutorials.html');" bgcolor=#ffffff text=#000000 link=#7F0000 vlink=#7F0000 alink=#7F0000 marginwidth=0 marginheight=0 leftmargin=0 topmargin=0>
<table height=100% cellspacing=0 cellpadding=0 border=0><tr>

<td valign=top>
<table width=566 cellspacing=0 cellpadding=20 border=0><tr><td>
<h1 style="margin-top:0px">Tutorial 04 - Rendering to texture</h1>
<p>
<b>I</b>n this tutorial we will learn how to use render targets.
<br><br>
<img src="t04_1.gif" width=150 height=113>&nbsp;&nbsp;
<br><br>
First, we declare the variables to handle the render target and
the sprite that we will associate with it:
</p>
<pre>
hgeSprite*   tar;

HTARGET      target;
</pre>
<p>
During some events like video mode switching, the render target's
texture handle may change. So, we write the GfxRestoreFunc function
in which we obtain the new render target's texture:
</p>
<pre>
bool GfxRestoreFunc()
{
  if(tar && target)
       tar->SetTexture(hge->Target_GetTexture(target));

  return false;
}
</pre>
<p>
In the <b>RenderFunc</b> we first render
all our stuff onto the texture, specifying our render target
in the <a href="hgefunc_gfxbeginscene.html">Gfx_BeginScene</a> call:
</p>
<pre>
  hge->Gfx_BeginScene(target);
  hge->Gfx_Clear(0);
  par->Render();
  spr->Render(x, y);
  hge->Gfx_EndScene();
</pre>
<p>
Then we render several instances of the texture to the screen:
</p>
<pre>
  hge->Gfx_BeginScene();
  hge->Gfx_Clear(0);

  for(i=0;i<6;i++)
  {
    tar->SetColor(0xFFFFFF | (((5-i)*40+55)<<24));
    tar->RenderEx(i*100.0f, i*50.0f, i*M_PI/8, 1.0f-i*0.1f);
  }

  fnt->printf(5, 5 ,HGETEXT_LEFT, "dt:%.3f\nFPS:%d",
              hge->Timer_GetDelta(), hge->Timer_GetFPS());
  hge->Gfx_EndScene();
</pre>
<p>
In the <b>WinMain</b> function we attach our GfxRestoreFunc function:
</p>
<pre>
  hge->System_SetState(HGE_GFXRESTOREFUNC, GfxRestoreFunc);
</pre>
<p>
Then, after <b>HGE</b> initiation, we create a render target and a sprite 
that we will use to make rendering of the created texture easier:
</p>
<pre>
    target=hge->Target_Create(512,512,false);

    tar=new hgeSprite(
           hge->Target_GetTexture(target),0,0,512,512);
    tar->SetBlendMode(
           BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
</pre>
<p>
During shutdown we delete the sprite that we used to render
the generated texture and the render target itself:
</p>
<pre>
    delete tar;
    hge->Target_Free(target);
</pre>
<p>
The rest of shutdown process is identical to the one demonstrated in
previous tutorials.
<br><br>
The complete source code with detailed comments for this tutorial you may find in the folder
<b>tutorials\tutorial04</b>. The required media files you'll find in the folder <b>tutorials\precompiled</b>.
</p>
<br>
</td></tr></table>
</td>

</tr></table>
</body>

</html>